import { getRoleConfig } from "utils/constants/bodyconstant";
import { CreepBoostSetup } from "utils/creep/CreepBoostSetup";

// 防守策略
export class DefendStrategy
{
        /**
     * 主防配置
     * @param stats 
     * @param enegycap 
     * @returns 
     */
    public createCreepBodyConfig(stats:IEnemyTeamSats,room:Required<Room>,maxboost:number=3):IWarPlan 
    {
        const plan =  this.getCombatPlan(stats.hostileCreeps,stats.attack+stats.rangeAttack,stats.groupCreeps);
        const bodyplan:[IBodyConfig, number][]=[]
        for(let k in plan)
        {
            switch(k)
            {
                case "attack":
                case "range":
                     const body = getRoleConfig("defRed",room.controller.level);
                    const bodyset = new Set(body.body.map(e=>e[0]));
                    for(let i=maxboost;i>=0;i--)
                    {
                        
                        if(i>0)
                        {
                            const minerials = CreepBoostSetup.getBoostMinerials(bodyset,"defRed",i as BoostLevel);
                            if(minerials.every(e=>room.getEntity('CoreEntity').resourceCount(e)>3000))
                            {
                                bodyplan.push([
                                    {
                                        Role:"defRed",
                                        boost:i,                    
                                        body:body.body
                                    },
                                    plan[k]
                                ])
                                break;
                            }
                        }
                        else 
                        {
                            bodyplan.push([
                                {
                                    Role:"defRed",
                                    boost:i,                    
                                    body:body.body
                                },
                                plan[k]
                            ])
                            break;
                        }
                        
                    }
            }
           
            
        }
        return {
            creep:bodyplan,
            team:[]
        }
    }


    /**
    * 获取战斗计划
    * @param enemys
    * @param damagesum
    * @param group
    * @returns
    */
    private getCombatPlan(enemys:Creep[],damagesum:number,group:Creep[][])
    {
        let n=1;
        let defend_plan:Record<string,number>= {}

        if (enemys.length < 2 || damagesum < 600)      // 1
        {
            defend_plan = { 'attack': 1* n, 'double': 0, 'range': 0 }
        } else if (enemys.length <= 2)      // 2
        {
            defend_plan = { 'attack': 1* n, 'double': 0, 'range': 0 }
        }
        else if (enemys.length > 2 && enemys.length < 5)       // 3-4
        {
            defend_plan = { 'attack': 2* n, 'double': 0, 'range': 0 }
        }
        else if (enemys.length >= 5 && enemys.length < 8)   // 5-7
        {
            defend_plan = { 'attack': 3* n, 'double': 0, 'range': 0 }
        }
        else if (enemys.length >= 8)        // >8     一般这种情况下各个类型的防御任务爬虫的数量都要调高
        {
            let attack_number = 2
            if (group.length > 4) {
                attack_number = group.length;
                attack_number = attack_number > 6 ? 6 : attack_number;
            }
            defend_plan = { 'attack': attack_number, 'double': 2, 'range': 0 }
        }
        return defend_plan;
    }
}